package com.sxz.sbs.util;

/**
 * @author : tian
 *         time   : 2018/03/09
 *         desc   :
 */

public class VersionUtils {
    /**
     * 比较版本号
     * @return 0 : 相同
     * return > 0: version1 > version2
     * return < 0: version1 < version2
     */
    public static int compareVersion(String version1, String version2) {
        if (version1.equals(version2)) {
            return 0;
        }
        String[] version1Array = version1.split("\\.");
        String[] version2Array = version2.split("\\.");

        int index = 0;
        int minLen = Math.min(version1Array.length, version2Array.length);
        int diff = 0;

        while (index < minLen && (diff = version1Array[index].length() - version2Array[index].length()) == 0
                && (diff = version1Array[index].compareTo(version2Array[index])) == 0) {
            index++;
        }
        if (diff != 0) {
            return diff;
        } else {
            return version1Array.length - version2Array.length;
        }
    }


}
